package com.project.service;

import com.project.dao.MajorMapper;
import com.project.entity.Major;
import com.project.entity.StudentInfo;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 14:10
 * @param:
 * @return:
 */
@Transactional(rollbackFor=Exception.class)
@Service
public class MajorServiceImpl extends BaseService implements MajorService {
    @Autowired
    private MajorMapper majorMapper;

    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassService classService;

    @Override
    public int addOne(Major major) {
        return majorMapper.insertSelective(major);
    }

    @Override
    public int remove(Integer id) {
        return majorMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int modify(Major major) {
        return majorMapper.updateByPrimaryKeySelective(major);
    }

    @Override
    public Major findOne(Integer id) {
        return majorMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017-6-22
     * function: 根据系id寻找专业
     *
     * @param departmentId 系id
     * @return
     */
    @Override
    public List<Major> findBatch(Integer departmentId) {
        return majorMapper.selectByDepartmentId(departmentId);
    }

    /**
     * author: WheelChen
     * date: 2017/8/10
     * function: 检查专业是否已存在
     *
     * @param major
     * @return true - 存在 false - 不存在
     */
    @Override
    public boolean checkMajorIsExist(Major major) {
        return majorMapper.checkMajorIsExist(major) != 0;
    }

    /**
     * author: WheelChen
     * date: 2017/10/24
     * function: 根据专业级联删除
     *
     * @param id
     * @param request
     * @return
     */
    @Override
    public boolean removeOneChainById(Integer id, HttpServletRequest request) {
        //删除专业表
        remove(id);
        //删除班级表
        classService.removeByMajorId(id);
        //查出班级下的学生表信息
        List<StudentInfo> studentInfoList = studentInfoService.findBatchByMajorId(id);

        if (studentInfoList.size() != 0)
            studentService.removeBatchChain(studentInfoList);
        return true;
    }

    /**
     * author: WheelChen
     * date: 2017/11/2
     * function: 根据系id批量删除专业表
     *
     * @param departmentId 系id
     * @return
     */
    @Override
    public int removeBatchByDepartmentId(Integer departmentId) {
        return majorMapper.deleteBatchByDepartmentId(departmentId);
    }

    /**
     * author: WheelChen
     * date: 2017/11/3
     * function: 根据学校id批量删除
     *
     * @param schoolId 学校id
     * @return
     */
    @Override
    public int removeBatchBySchoolId(Integer schoolId) {
        return majorMapper.deleteBatchBySchoolId(schoolId);
    }
}
